package com.yugao.fintech.draper.core.validator;

import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import com.yugao.fintech.draper.core.annotation.CheckMobileValid;
import com.yugao.fintech.draper.core.constant.RegexpConstant;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

public class MobileValidator implements ConstraintValidator<CheckMobileValid, String> {

    @Override
    public void initialize(CheckMobileValid isMobile) {
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        try {
            if (StrUtil.isBlank(s)) {
                return true;
            } else {
                String regex = RegexpConstant.MOBILE_REG;
                return ReUtil.isMatch(regex, s);
            }
        } catch (Exception e) {
            return false;
        }
    }
}